package com.crysta1.picviewer;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.github.chrisbanes.photoview.PhotoView;

public class DetailFragment extends Fragment {
    private PhotoView photoView;

    public static DetailFragment newInstance(String imageUrl) {
        DetailFragment fragment = new DetailFragment();
        Bundle args = new Bundle();
        args.putString("url", imageUrl);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_detail, container, false);
        photoView = view.findViewById(R.id.photo_view);
        return view;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 初始化 PhotoView
        photoView = view.findViewById(R.id.photo_view);

        // 添加双击监听器
        photoView.setOnDoubleTapListener(new CustomDoubleTapListener(photoView));

        // 加载图片
        loadImage();
    }

    private void loadImage() {
        String url = getArguments().getString("url");
        Glide.with(this)
                .load(url)
                .apply(new RequestOptions()
                        .fitCenter()
                        .error(R.drawable.ic_error)
                )
                .into(photoView);

        // 配置缩放参数
        photoView.setScale(1f, false);
        photoView.setMaximumScale(8f);
        photoView.setMediumScale(3f);
    }


}